{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b17dcf3-aee3-4ae0-81bf-7339f0111534",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "from linearmodels.panel import PanelOLS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47b6bab5-4eef-4483-9718-2ac4781da919",
   "metadata": {},
   "source": [
    "# 1. Analysis 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4df2e0d9-2e3f-42a4-9ab7-c6edb9ade3be",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"\\df.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed12ddd5-54ef-4ddc-93ef-bd91b945f592",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define the dependent variable (Y)\n",
    "y = df[\"Climate_Change\"]\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [ \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcde8d2b-f646-48fa-af29-783ed893ed6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define the dependent variable (Y)\n",
    "y = df[\"Climate_Change\"]\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"Environment_Stress_Index\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b812aded-5494-4eab-8c4d-c98b619b1e78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define the dependent variable (Y)\n",
    "y = df[\"Climate_Change\"]\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"Heat_Index\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f6302c3-e216-490e-9dae-4d77c9192f19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define the dependent variable (Y)\n",
    "y = df[\"Climate_Change\"]\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"WBGT_JME\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03d58851-ca92-4f0a-92c0-7d49d17fcae5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ab94f6f6-8187-4133-81be-95ca6345567f",
   "metadata": {},
   "source": [
    "# Analysis 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "790b1bd1-45d4-4eb6-91dd-fdd4f4420f96",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"\\df.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "880e7b76-9475-4c9d-881e-9999efc8bd85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Drop rows where either conservative or liberal sentiment score is missing\n",
    "df = df.dropna(subset=[\"Sentiment_Score_Climate_Change_Conservative\", \"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define dependent variable (Absolute Sentiment Distance)\n",
    "y = np.abs(df[\"Sentiment_Score_Climate_Change_Conservative\"] - df[\"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [ \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abc8790c-6dcc-4bf4-9084-2215461d300d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Drop rows where either conservative or liberal sentiment score is missing\n",
    "df = df.dropna(subset=[\"Sentiment_Score_Climate_Change_Conservative\", \"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define dependent variable (Absolute Sentiment Distance)\n",
    "y = np.abs(df[\"Sentiment_Score_Climate_Change_Conservative\"] - df[\"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"Environment_Stress_Index\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44e27931-f679-4727-943c-a4441f556775",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Drop rows where either conservative or liberal sentiment score is missing\n",
    "df = df.dropna(subset=[\"Sentiment_Score_Climate_Change_Conservative\", \"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define dependent variable (Absolute Sentiment Distance)\n",
    "y = np.abs(df[\"Sentiment_Score_Climate_Change_Conservative\"] - df[\"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"Heat_Index\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f79399f8-7134-4cd3-99be-1f1c76964422",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set panel data index (District & Year)\n",
    "df.set_index([\"District\", \"Year\"], inplace=True)\n",
    "\n",
    "# Drop rows where either conservative or liberal sentiment score is missing\n",
    "df = df.dropna(subset=[\"Sentiment_Score_Climate_Change_Conservative\", \"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Convert Month into dummy variables (Month Fixed Effects)\n",
    "df = pd.get_dummies(df, columns=[\"Month\"], drop_first=True)\n",
    "\n",
    "# Define dependent variable (Absolute Sentiment Distance)\n",
    "y = np.abs(df[\"Sentiment_Score_Climate_Change_Conservative\"] - df[\"Sentiment_Score_Climate_Change_Liberal\"])\n",
    "\n",
    "# Define independent variables (X) including Month Fixed Effects\n",
    "x_vars = [\"WBGT_JME\", \"Ratio_of_Conservatives\"] + [col for col in df.columns if \"Month_\" in col]\n",
    "X = df[x_vars]\n",
    "\n",
    "# Add a constant term\n",
    "X = sm.add_constant(X)\n",
    "\n",
    "# Run Three-Way Fixed Effects Regression Model\n",
    "model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(model.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d63cbce-716e-4d3c-8f89-ac46876a1494",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9b62c2b-2984-404c-891e-2941c251bf4d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8ea5e14-2c11-44e7-84b5-ee50ee6d9c37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73cef9c5-9d39-42c0-8733-c16049ab9fe3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d0b2bb9-d2b5-43ce-9fec-9a55056bb0ae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9759e514-15c8-43c1-9498-206684dbd81f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
